package com.lesso.zgs.sunflower.controller;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/demo-simple")
public class DemoSimpleController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 1. Redis 基础 set/get
    @GetMapping("/redis/set")
    public String redisSet(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return "OK";
    }
    @GetMapping("/redis/get")
    public String redisGet(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 2. Redisson 分布式锁
    @GetMapping("/redisson/lock")
    public String redissonLock() {
        RLock lock = redissonClient.getLock("myLock");
        lock.lock();
        try {
            Thread.sleep(1000); // 模拟业务
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return "locked and released";
    }

    // 3. RabbitMQ 发送消息
    @GetMapping("/mq/send")
    public String mqSend(String msg) {
        rabbitTemplate.convertAndSend("test-queue", msg);
        return "sent: " + msg;
    }

    // 4. Redis Geo add/search
    @GetMapping("/geo/add")
    public String geoAdd(String key, double lng, double lat, String member) {
        redisTemplate.opsForGeo().add(key, new Point(lng, lat), member);
        return "added";
    }

} 